package cn.dao789.mvp;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import cn.dao789.R;

/**
 * MVP架构中的View实现类
 * 负责UI的渲染和用户交互，通过Presenter处理业务逻辑
 */
public class MvpActivity extends AppCompatActivity implements UserView {
    // View组件
    private EditText editTextUsername;
    private EditText editTextEmail;
    private Button buttonSave;
    private TextView textViewUserInfo;
    
    // Presenter
    private UserPresenter userPresenter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mvp);
        
        // 初始化View组件
        editTextUsername = findViewById(R.id.editTextUsername);
        editTextEmail = findViewById(R.id.editTextEmail);
        buttonSave = findViewById(R.id.buttonSave);
        textViewUserInfo = findViewById(R.id.textViewUserInfo);
        
        // 创建Presenter
        userPresenter = new UserPresenter(this);
        
        // 初始化View
        userPresenter.initView();
        
        // 设置事件监听
        buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 委托给Presenter处理
                userPresenter.saveUser();
            }
        });
    }
    
    @Override
    public void showUserInfo(String userId, String username, String email) {
        // 更新UI显示用户信息
        textViewUserInfo.setText("用户信息：\n" +
                "ID: " + userId + "\n" +
                "用户名: " + username + "\n" +
                "邮箱: " + email);
    }
    
    @Override
    public String getUsernameInput() {
        return editTextUsername.getText().toString();
    }
    
    @Override
    public String getEmailInput() {
        return editTextEmail.getText().toString();
    }
    
    @Override
    public void showSaveSuccessMessage() {
        Toast.makeText(this, "保存成功！", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 释放Presenter中的View引用
        userPresenter.onDestroy();
    }
}